package ro.gateway.aida.locations;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.CRegion;
import ro.gateway.aida.obj.db.AIDALocationDB;
import ro.gateway.aida.obj.db.CountyDB;
import ro.gateway.aida.obj.db.RegionDB;
import ro.gateway.aida.obj.db.TownDB;
import ro.gateway.aida.obj.db.VillageDB;
import ro.gateway.aida.utils.HttpUtils;
import ro.gateway.aida.utils.Utils;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: LocationEditorAction.java,v 1.1 2004/10/24 23:37:41 mihaipostelnicu Exp $
 */
public class LocationEditorAction extends Action {
     public ActionForward execute( ActionMapping mapping,
                                   ActionForm form_,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
        throws Exception {
          PersistenceToken token = PersistenceManager.
             tokenLookup( this.getServlet().getServletContext() );
          LocationEditorForm form = ( LocationEditorForm ) form_;
          form.action = HttpUtils.getValidTrimedString( request, "action", "chgcregion" );

          if ( form.action.equals( "chgcregion" ) ) {
               form.clear();
               form.cregion_id = HttpUtils.getInt( request, "cregion_id", -1 );
               try {
                    form.cregion = AIDALocationDB.getManager( token ).getCRegionById( form.cregion_id );
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( form.action.equals( "chgcountry" ) ) {
               form.clear();
               form.country_iso3 = HttpUtils.getValidTrimedString( request, "country_iso3", null );
               try {
                    form.country = AIDALocationDB.getManager( token ).getCountryByISO3( form.country_iso3 );
//                        System.out.println( "found country:" + (form.country != null ? form.country.getName() : "") );
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( form.action.equals( "chgregion" ) ) {
               form.clear();
               form.region_id = HttpUtils.getInt( request, "region_id", -1 );
               try {
                    form.region = RegionDB.getManager( token ).getById( form.region_id );
//                        System.out.println( "found region:" + (form.region != null ? form.region.getName() : "") );
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( form.action.equals( "chgcounty" ) ) {
               form.clear();
               form.county_id = HttpUtils.getInt( request, "county_id", -1 );
               try {
                    form.county = CountyDB.getManager( token ).getById( form.county_id );
//                        System.out.println( "found county:" + (form.county != null ? form.county.getName() : "") );
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( form.action.equals( "chgtown" ) ) {
               form.clear();
               form.town_id = HttpUtils.getInt( request, "town_id", -1 );
               try {
                    form.town = TownDB.getManager( token ).getById( form.town_id );
//                        System.out.println( "found town:" + (form.town != null ? form.town.getName() : "") );
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( form.action.equals( "chgvillage" ) ) {
               form.clear();
               form.village_id = HttpUtils.getInt( request, "village_id", -1 );
               try {
                    form.village = VillageDB.getManager( token ).getById( form.village_id );
//                        System.out.println( "found village:" + (form.village != null ? form.village.getName() : "") );
               } catch ( Exception ex ) {
                    ex.printStackTrace();
               }
          } else if ( form.action.equals( "edit" ) ) {
          } else if ( form.action.equals( "delete" ) ) {
          } else if ( form.action.equals( "create" ) ) {
          }

          try {
               if ( form.village != null ) {
                    form.town = TownDB.getManager( token )
                       .getById( form.village.getSirsup_id() );
                    form.town_id = form.village.getSirsup_id();
               }
               if ( form.town != null ) {
                    form.county = CountyDB.getManager( token )
                       .getById( form.town.getJudet_id() );
                    form.county_id = form.town.getJudet_id();
               }
               if ( form.county != null ) {
                    form.region = RegionDB.getManager( token )
                       .getById( form.county.getRegiune_id() );
                    form.region_id = form.county.getRegiune_id();
               }
               if ( form.region != null ) {
                    form.country = AIDALocationDB.getManager( token )
                       .getCountryByISO3( form.region.getCountryIso3() );
                    form.country_iso3 = form.region.getCountryIso3();
               }
               if ( form.country != null ) {
                    form.cregion = AIDALocationDB.getManager( token )
                       .getCRegionById( form.country.getCregion_id() );
                    form.cregion_id = form.country.getCregion_id();
//                        System.out.println( "cregion:" + form.cregion_id + "," + form.cregion );
               }
          } catch ( Exception ex ) {
               ex.printStackTrace();
          }


          // updatez listele
          if ( form.action.startsWith( "chg" ) ) {
               form.cregions = make_tree_list( AIDALocationDB.getManager( token ).getRootCregions( true ) );


               if ( form.cregions != null ) {
                    request.setAttribute( "cregions", form.cregions );
               }
               if ( form.cregion != null ) {
                    form.countries = AIDALocationDB.getManager( token )
                       .getCountriesByCRegion( form.cregion.getId() );
                    if ( form.countries != null ) {
                         request.setAttribute( "countries", form.countries );
                    }
               }
               if ( form.country != null ) {
                    form.regions = RegionDB.getManager( token )
                       .getAllByCountry( form.country.getIso3() );
                    if ( form.regions != null ) {
                         request.setAttribute( "regions", form.regions );
                    }
               }
               if ( form.region != null ) {
                    form.counties = CountyDB.getManager( token )
                       .getForRegion( form.region.getId() );
                    if ( form.counties != null ) {
                         request.setAttribute( "counties", form.counties );
                    }
               }
               if ( form.county != null ) {
                    form.towns = TownDB.getManager( token )
                       .getAllForCounty( form.county.getId() );
                    if ( form.towns != null ) {
                         request.setAttribute( "towns", form.towns );
                    }
               }
               if ( form.town != null ) {
                    form.villages = VillageDB.getManager( token )
                       .getAllForTown( form.town.getSirsup_id() );
                    if ( form.villages != null ) {
                         request.setAttribute( "villages", form.villages );
                    }
               }
          }
          Utils.setBreadCrumbs( request, "bc.locman" );
          return mapping.findForward( "jsp_page" );
     }

     static CRegion[] make_tree_list( CRegion[] cregions ) {
          ArrayList items = new ArrayList();

          for ( int i = 0; i < cregions.length; i++ ) {
               CRegion cregion = cregions[ i ];
               items.add( cregion );
               CRegion[] children = cregions[ i ].getChildren();
               if ( children != null )
                    for ( int j = 0; j < children.length; j++ ) {
                         items.add( children[ j ] );
                    }
          }

          if ( items.size() > 0 ) {
               CRegion[] result = new CRegion[ items.size() ];
               items.toArray( result );
               return result;
          }
          return null;
     }
}
